#include <semaphore.hpp>

Semaphore::Semaphore(int value)
{
#if defined(CONFIG_TAR4ESP32)
	m_Semaphore = xSemaphoreCreateCounting(65535, value);
#else
	sem_init(&m_Semaphore, 0, value);
#endif
}

Semaphore::~Semaphore()
{
#if defined(CONFIG_TAR4ESP32)
	vSemaphoreDelete(m_Semaphore);
#else
	sem_destroy(&m_Semaphore);
#endif
}

int Semaphore::Wait(void)
{
#if defined(CONFIG_TAR4ESP32)
	return (int)xSemaphoreTake(m_Semaphore, 0xFFFFFFFF);
#else
	return sem_wait(&m_Semaphore);
#endif
}

int Semaphore::Post(void)
{
#if defined(CONFIG_TAR4ESP32)
	return (int)xSemaphoreGive(m_Semaphore);
#else
	return sem_post(&m_Semaphore);
#endif
}